﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using MM.DBUtility;
using MM.Common;


namespace MobileManager
{
    public partial class SProChanel : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurUser"] == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
            if (!IsPostBack)
            {
                BindDdlChannel(ref ddlChannel);
                ddlChannel.Items.Insert(0, new ListItem("全部", "-1"));

                inputDate.Value = DateTime.Now.ToString("yyyy-MM-dd");

                GVDataBind();
            }
        }
        private void BindDdlChannel(ref DropDownList ddl)
        {
            ddl.DataSource = DbHelperSql.Query("SELECT a.ChannelNO,a.ChannelNO+'-'+ a.ChannelNumber+'-'+a.ChannelOrder+'-'+b.SPName as Name FROM T_Channel a,T_SPbusi b WITH(NOLOCK) WHERE a.SNO=b.SNO order by a.ChannelNO ").Tables[0];
            ddl.DataValueField = "ChannelNO";
            ddl.DataTextField = "Name";
            ddl.DataBind();
        }
        private void GVDataBind()
        {

            lbTime1.Text = inputDate.Value + "开通情况:";
            lbTime2.Text = Convert.ToDateTime(inputDate.Value).AddDays(1).ToString("yyyy-MM-dd") + "开通情况:";

            string strSQL = string.Format("SELECT DISTINCT A.ProvinceID,A.ChannelNO,A.ONO,B.ProvinceName FROM T_Rules A,T_Province B WITH(NOLOCK) WHERE A.ProvinceID=B.ProvinceID AND A.NextTime='{0}'", inputDate.Value);
            if (!"-1".Equals(ddlChannel.SelectedValue))
            {
                strSQL += string.Format(" AND A.ChannelNO='{0}'", ddlChannel.SelectedValue);
            }

            DataTable dt = DbHelperSql.Query(strSQL).Tables[0];


            string strProNamesL = string.Empty, strProNamesY = string.Empty, strProNamesL2 = string.Empty, strProNamesY2 = string.Empty, strProIDsL = string.Empty, strProIDsY = string.Empty;

            //移动
            DataRow[] rowsY = dt.Select("ONO='1'", "ProvinceName");
            foreach (DataRow row in rowsY)
            {
                strProNamesY += row["ProvinceName"].ToString() + "(" + row["ChannelNO"].ToString() + ")" + ",";
                strProIDsY += row["ProvinceID"].ToString() + ",";
            }
            if (!string.IsNullOrEmpty(strProIDsY))
            {
                DataTable dt2 = DbHelperSql.Query(string.Format("SELECT ProvinceName FROM T_Province WITH(NOLOCK) WHERE ProvinceID NOT IN ({0})", strProIDsY.Remove(strProIDsY.LastIndexOf(",")))).Tables[0];
                rowsY = dt2.Select("", "ProvinceName");

                foreach (DataRow row in rowsY)
                {
                    strProNamesY2 += row["ProvinceName"].ToString() + ",";
                }
            }

            //联通
            DataRow[] rowsL = dt.Select("ONO='2'", "ProvinceName");
            foreach (DataRow row in rowsL)
            {
                strProNamesL += row["ProvinceName"].ToString() + "(" + row["ChannelNO"].ToString() + ")" + ",";
                strProIDsL += row["ProvinceID"].ToString() + ",";
            }

            if (!string.IsNullOrEmpty(strProIDsL))
            {
                DataTable dt2 = DbHelperSql.Query(string.Format("SELECT ProvinceName FROM T_Province WITH(NOLOCK) WHERE ProvinceID NOT IN ({0})", strProIDsL.Remove(strProIDsL.LastIndexOf(",")))).Tables[0];
                rowsL = dt2.Select("", "ProvinceName");

                foreach (DataRow row in rowsL)
                {
                    strProNamesL2 += row["ProvinceName"].ToString() + ",";
                }
            }
            if (!string.IsNullOrEmpty(strProNamesY))
                strProNamesY = strProNamesY.Remove(strProNamesY.LastIndexOf(","));
            if (!string.IsNullOrEmpty(strProNamesY2))
                strProNamesY2 = strProNamesY2.Remove(strProNamesY2.LastIndexOf(","));
            if (!string.IsNullOrEmpty(strProNamesL))
                strProNamesL = strProNamesL.Remove(strProNamesL.LastIndexOf(","));
            if (!string.IsNullOrEmpty(strProNamesL2))
                strProNamesL2 = strProNamesL2.Remove(strProNamesL2.LastIndexOf(","));

            //移动
            lbPorOpenY.Text = strProNamesY;
            lbPorCloseY.Text = strProNamesY2;
            //联通
            lbPorOpenL.Text = strProNamesL;
            lbPorCloseL.Text = strProNamesL2;

            //明天
            strSQL = string.Format("SELECT DISTINCT A.ProvinceID,A.ChannelNO,A.ONO,B.ProvinceName FROM T_Rules A,T_Province B WITH(NOLOCK) WHERE A.ProvinceID=B.ProvinceID AND A.NextTime='{0}'", Convert.ToDateTime(inputDate.Value).AddDays(1).ToString("yyyy-MM-dd"));
            if (!"-1".Equals(ddlChannel.SelectedValue))
            {
                strSQL += string.Format(" AND A.ChannelNO='{0}'", ddlChannel.SelectedValue);
            }

            dt = DbHelperSql.Query(strSQL).Tables[0];
            string strProNamesYed = string.Empty, strProNamesLed = string.Empty;
            //移动已经开通
            rowsY = dt.Select("ONO='1'", "ProvinceName");
            foreach (DataRow row in rowsY)
            {
                strProNamesYed += row["ProvinceName"].ToString() + "(" + row["ChannelNO"].ToString() + ")" + ",";
            }

            rowsL = dt.Select("ONO='2'", "ProvinceName");
            foreach (DataRow row in rowsL)
            {
                strProNamesLed += row["ProvinceName"].ToString() + "(" + row["ChannelNO"].ToString() + ")" + ",";
            }

            if (!string.IsNullOrEmpty(strProNamesYed))
                strProNamesYed = strProNamesYed.Remove(strProNamesYed.LastIndexOf(","));
            if (!string.IsNullOrEmpty(strProNamesLed))
                strProNamesLed = strProNamesLed.Remove(strProNamesLed.LastIndexOf(","));

            lbPorOpenY2.Text = strProNamesYed;
            lbPorOpenL2.Text = strProNamesLed;

        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(inputDate.Value))
            {
                MM.Common.Common.PageStartAlert(this.Page, "请选择日期！");
                return;
            }
            else
                GVDataBind();
        }
    }
}
